<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xhtml="http://www.w3.org/1999/xhtml"
	xmlns:f="livcos.org/ns/xslt/function"
	xmlns:m="livcos.org/ns/map"
	xmlns:doc="livcos.org/ns/doc"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:v="livcos.org/ns/view"
	exclude-result-prefixes="xhtml f m doc v">
	
	<xsl:import href="base.xsl"/>
	<xsl:import href="../doc/content.xsl"/>
	<xsl:import href="../doc/ns/livcos.org/ns/doc/link.xsl"/>
	<xsl:import href="../style/web.xsl"/>
	
	<xsl:param name="map"/>
	<xsl:param name="item"/>
	<xsl:param name="ref" select="$map"/>
	<xsl:param name="id" select="$item"/>
	<xsl:param name="rev"/>
	<xsl:param name="contentViewObjId" select="resolve-uri('../doc')"/>
	<xsl:param name="incl-editor" select="if ($rev) then '0' else '1'"/>
	
	<xsl:variable name="prepBaseUris" select="resolve-uri('ns', $taskDefId), resolve-uri('ns')"/>
	<xsl:variable name="contentBaseUris" select="resolve-uri('../doc/ns', $taskDefId), resolve-uri('../doc/ns')"/>
	
	<xsl:variable name="MAX_MAIN_LABEL_LENGTH" select="40"/>
	<xsl:variable name="MAX_LABEL_LENGTH" select="30"/>
	<xsl:variable name="MAX_LINK_LABEL_LENGTH" select="40"/>
	
	<xsl:template match="/">
		<xsl:variable name="_objId" select="f:find-data-object($ref)"/>
		<xsl:choose>
			<xsl:when test="$_objId != ''">
				<xsl:variable name="prep">
					<xsl:variable name="entities" select="doc($_objId)//*[@id]"/>
					<xsl:variable name="entity" select="if ($id) then $entities[@id = $id] else $entities[1]"/>
					<xsl:variable name="defObjUri" select="f:object-uri-from-namespace(namespace-uri($entity))"/>
					<xsl:variable name="defObjIds" select="for $d in $prepBaseUris return f:find-object($d, $defObjUri, 'view')"/>
					<xsl:sequence select="f:transform(($defObjIds[f:object-available(.)])[1], $entity)"/>
				</xsl:variable>
				<xsl:apply-templates select="$prep/*" mode="root"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:sequence select="doc(concat('/livcos.org/web/sys/feedback/ObjectNotFound?objUri=', $ref))/*"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="m:item" mode="root">
		<xsl:variable name="content">
			<xsl:call-template name="apply_content"/>
		</xsl:variable>
		<html>
			<head>
				<title><xsl:value-of select="m:label"/></title>
				<meta name="to-root-path" content="{$toRootPath}"/>
				<meta name="view-object-id" content="{$viewObjId}"/>
				<link href="{f:caller-uri(resolve-uri('css/styles.css'))}" rel="stylesheet" type="text/css" />
				<link href="{f:caller-uri(resolve-uri('../edit/css/styles.css'))}" rel="stylesheet" type="text/css" />
				<xsl:for-each select="distinct-values($content//v:style/@ref)">
					<link href="{f:caller-uri(.)}" rel="stylesheet" type="text/css" />
				</xsl:for-each>
				<script type="text/javascript" src="{f:caller-uri(resolve-uri('../doc/script/scripts.js'))}">
					<xsl:comment> </xsl:comment></script>
				<script type="text/javascript" src="{f:caller-uri(resolve-uri('script/map.js'))}">
					<xsl:comment> </xsl:comment></script>
				<xsl:if test="$incl-editor = '1'">
					<script type="text/javascript" src="{f:caller-uri(resolve-uri('../edit/script/scripts.js'))}">
						<xsl:comment> </xsl:comment></script>
					<script type="text/javascript" src="{f:caller-uri(resolve-uri('script/scripts.js'))}">
						<xsl:comment> </xsl:comment></script>
				</xsl:if>
			</head>
			<body>
				<div class="contentPanel">
					<table cellspacing="0" cellpadding="0">
						<tbody>
							<tr>
								<td class="left" rowspan="2">
									<div class="rels">
										<xsl:call-template name="apply_left"/>
									</div>
								</td>
								<td class="top">
									<div id="{@id}" class="mainItem">
										<xsl:value-of select="substring(m:label, 0, $MAX_MAIN_LABEL_LENGTH)"/>
										<xsl:if test="string-length(m:label) &gt;= $MAX_MAIN_LABEL_LENGTH">...</xsl:if>
									</div>
								</td>
								<td class="right" rowspan="2">
									<div class="rels">
										<xsl:call-template name="apply_right"/>
									</div>
								</td>
							</tr>
							<tr>
								<td class="center">
									<div class="content" id="{@id}">
										<xsl:apply-templates select="$content/*"/>
									</div>
								</td>
							</tr>
						</tbody>
					</table>
				</div>
				<xsl:variable name="mapId" select="f:object-id(.)"/>
				<div id="{$mapId}" class="map">
					<a href="{f:caller-uri(concat($viewObjId, $mapId, '.html'))}">
						<xsl:value-of select="m:map-title"/>
						<xsl:if test="normalize-space(m:map-title) = ''">
							<xsl:value-of select="$mapId"/>
						</xsl:if>
					</a>
				</div>
				<div id="_insert" style="display: none;">
					<form style="margin: 0px; padding: 0px;">
						<table class="ed_body">
							<col style="width: 4em"/>
							<tbody>
								<tr>
									<td class="ed_label"><label for="label">Label:</label></td>
									<td class="ed_entry">
										<div style="position: relative">
											<input name="label" type="text" autocomplete="off" style="width: 100%"
												value="-- label --"/>
										</div>
									</td>
								</tr>
								<tr>
									<td class="ed_label"><label for="uri">URI:</label></td>
									<td class="ed_entry">
										<div style="position: relative">
											<input name="uri" type="text" autocomplete="off" style="width: 100%"
												value="{substring-before(@id, '#')}"/>
										</div>
									</td>
								</tr>
							</tbody>
						</table>
						<div class="ed_footer">
							<input type="submit" value="Create" class="ed_action"/>
							<input type="button" onclick="this.form._panel.close();" value="Cancel" class="ed_action"/>
						</div>
					</form>
				</div>
			</body>
		</html>
	</xsl:template>
	
	<xsl:template name="apply_left">
		<xsl:apply-templates select="m:rels[not(@group) or (@group != 'main')]/*"/>
	</xsl:template>
	
	<xsl:template name="apply_right">
		<xsl:apply-templates select="m:rels[@group = 'main']/*"/>
	</xsl:template>
	
	<xsl:template name="apply_content">
		<xsl:variable name="nsViews" select="f:find-objs-for-namespaces(., $contentBaseUris, 'view')"/>
		<xsl:sequence select="f:transform('/livcos.org/web/map/content', ., ('viewObjId', $contentViewObjId), $nsViews)"/>
	</xsl:template>
	
	<xsl:template match="m:rel">
		<div>
			<xsl:apply-templates select="m:item"/>
		</div>
	</xsl:template>
	
	<xsl:template match="/m:item/m:rels/m:rel">
		<table class="rel">
			<tbody>
				<tr>
					<xsl:choose>
						<xsl:when test="../@group = 'main'">
							<td class="right1">
								<xsl:apply-templates select="m:item"/>
							</td>
							<td class="right2">
								<xsl:apply-templates select="m:item/m:rels/m:rel"/>
							</td>
						</xsl:when>
						<xsl:otherwise>
							<td class="left2">
								<xsl:apply-templates select="m:item/m:rels/m:rel"/>
							</td>
							<td class="left1">
								<xsl:apply-templates select="m:item"/>
							</td>
						</xsl:otherwise>
					</xsl:choose>
				</tr>
			</tbody>
		</table>
	</xsl:template>
	
	<xsl:template match="m:rel[m:item/@id = /m:item/@id]" priority="2">
	</xsl:template>
	
	<xsl:template match="m:item">
		<xsl:variable name="level" select="count(ancestor::m:item)"/>
		<div id="{../@id}" class="item {if (@missing) then 'missing' else ''} rel{$level}">
			<xsl:if test="doc:link">
				<xsl:variable name="lnk" as="element()">
					<xsl:variable name="l" select="doc:link[1]"/>
					<doc:link id="{f:entity-id($l)}" view="{$contentViewObjId}">
						<xsl:copy-of select="$l/@* except $l/@id"/>
						<xsl:copy-of select="$l/node()"/>
					</doc:link>
				</xsl:variable>
				<a class="directLink" href="{f:link-to-url($lnk)}" title="Directly navigate link..."
					>&#160;</a>
			</xsl:if>
			<a href="{f:caller-uri(concat($viewObjId, replace(@id, '#', '\$'), '.html'))}" class="">
				<xsl:if test="($level = 1) and ../@type">
					<xsl:choose>
						<xsl:when test="../../@group = 'main'">
							<img src="{f:caller-uri(resolve-uri(concat('img/', ../@type, 'Right.png')))}"
								style="margin: 1px -24px 0px 8px; float: left"/>
						</xsl:when>
						<xsl:otherwise>
							<img src="{f:caller-uri(resolve-uri(concat('img/', ../@type, 'Left.png')))}"
								style="margin: 1px 8px 0px -24px; float: right"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:if>
				<xsl:value-of select="substring(m:label, 0, $MAX_LABEL_LENGTH)"/>
				<xsl:if test="string-length(m:label) &gt;= $MAX_LABEL_LENGTH">...</xsl:if>
			</a>
			<xsl:apply-templates select="../m:comment" mode="tooltip"/>
			<xsl:if test="not(../m:comment)">
				<xsl:apply-templates select="doc:paragraph[1]" mode="tooltip"/>
				<xsl:if test="not(doc:paragraph)">
					<xsl:apply-templates select="doc:*[1]" mode="tooltip"/>
				</xsl:if>
			</xsl:if>
		</div>
	</xsl:template>
	
	<xsl:template match="m:comment | doc:*" mode="tooltip">
		<div class="tooltip"><xsl:value-of select="."/></div>
	</xsl:template>
	
	<xsl:template match="doc:link[. = '']" mode="tooltip">
		<div class="tooltip"><xsl:value-of select="@ref"/></div>
	</xsl:template>
	
</xsl:stylesheet>
